package com.commerce.weather.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.commerce.weather.Entity.City;
import com.commerce.weather.R;
import com.commerce.weather.adapter.MyTextAdapter;
import com.commerce.weather.utils.JsonToType;

import java.util.ArrayList;
import java.util.List;

public class ReActivity extends AppCompatActivity {

    private Button button;
    private EditText editText;
    private ListView listView;
    private List<City.ResultBean> cityClone = new ArrayList<>();
    private City city;
    private MyTextAdapter myTextAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_re);
        button=findViewById(R.id.re_collectionBtn);
        editText=findViewById(R.id.re_greetText);
        listView=findViewById(R.id.re_list_view);
        city = new JsonToType().ToCity(this);
        dataFutter();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String district = cityClone.get(i).getDistrict();
                setResult(200,new Intent().putExtra("district",district));
                finish();
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dataFutter();

            }
        });
    }

    private void dataFutter(){
        cityClone.clear();
        //搜索框
        String s = editText.getText().toString();
        for (int i=0;i<city.getResult().size();i++){
            //判断是否包含搜索内容
            if(city.getResult().get(i).getDistrict().contains(s)||
                    city.getResult().get(i).getProvince().contains(s)||
                    city.getResult().get(i).getCity().contains(s)){
                //添加到城市列表
                cityClone.add(city.getResult().get(i));
            }
        }
        if(myTextAdapter==null){
            myTextAdapter = new MyTextAdapter(this, cityClone);
            listView.setAdapter(myTextAdapter);
        }else {
            //通知listView控件更新数据
            myTextAdapter.notifyDataSetChanged();
        }


    }

}